/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidorped;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * @author Micosil
 */
public class UserList {

    private List<User> connectedUserList = new ArrayList();
    private final ReadWriteLock userListLock = new ReentrantReadWriteLock(true);

    public void connectUser(User user) {
        userListLock.writeLock().lock();
        try {
            connectedUserList.add(user);
        } finally {
            userListLock.writeLock().unlock();
        }
    }

    public List<User> getConnectedUsers() {
        userListLock.readLock().lock();
        try {
            return connectedUserList;
        } finally {
            userListLock.readLock().unlock();
        }
    }

    public void disconnectUser(User user) {
        userListLock.writeLock().lock();
        try {
            connectedUserList.remove(user);
        } finally {
            userListLock.writeLock().unlock();
        }
    }

    public User getUserByName(String userName) {
        userListLock.readLock().lock();
        try {
            for (User oneUser : connectedUserList) {
                if (oneUser.getUserName().equals(userName)) {
                    return oneUser;
                }
            }
            return null;
        } finally {
            userListLock.readLock().unlock();
        }
    }
}
